var { formatDate } = require("../../public/js/common.js");
var API = require('../../fetch/index.js');
var app = getApp();
const http = require('../../http/index');
const APICONSTE = require('../../http/apiConst');

Page({
  data: {
    id: '',
    today: formatDate(new Date()),
    accountMark: '',
    accountName: '',
    accountVal: '',
    accountType: '1',
    accountTypeName: '餐饮',
    time: formatDate(new Date()),
    accountTypes: [],
    imgWidth: 690,
    imgHeight: 0,
    fileid: '',

    items: [
      { name: 'pay', value: '支出', checked: true },
      { name: 'income', value: '收入' }
    ],
    seleted: "pay", // income pay

    incomeTypes: [
      { value: '0', name: '工资' },
      { value: '1', name: '兼职' },
      { value: '2', name: '理财' },
      { value: '3', name: '礼金' },
      { value: '4', name: '红包' },
      { value: '99', name: '其他' }
    ],
    incomeType: '0',
    incomeName: '工资',
    incomeValue: '',
    incomeTime: formatDate(new Date()),
    incomeMark: ''
  },

  checkAddIncome() {
    var reg = /^[0-9]+.?[0-9]*$/;
    var { incomeValue } = this.data;
    if (!incomeValue) {
      return '请填写金额';
    }
    if (!reg.test(incomeValue)) {
      return '请填写有效金额';
    }
    return null;
  },

  async addIncome() {
    const msg = this.checkAddIncome();
    if (msg) {
      wx.showToast({
        icon: 'none',
        title: msg
      });
      return;
    }
    var { incomeType, incomeValue, incomeTime, incomeMark } = this.data;
    await http({
      action: APICONSTE.ADD_INCOME,
      data: {
        incomeType,
        incomeValue: incomeValue / 1,
        incomeTime,
        incomeMark
      }
    });
    setTimeout(() => {
      wx.navigateBack();
    }, 300);
    this.setData({
      incomeValue: '',
      incomeMark: ''
    });
  },

  inputIncomeMark(e) {
    this.setData({
      incomeMark: e.detail.value
    });
  },

  changeIncomeTime(e) {
    this.setData({
      incomeTime: e.detail.value
    });
  },

  inputIncomeValue(e) {
    this.setData({
      incomeValue: e.detail.value
    });
  },

  changeIncomeType(e) {
    var val = e.detail.value;
    var { incomeTypes } = this.data;
    const income = incomeTypes.find(i => (i.value == val));
    this.setData({
      incomeType: income.value,
      incomeName: income.name
    });
  },

  radioChange(e) {
    this.setData({
      seleted: e.detail.value
    })
  },

  setAccountTypeName() {
    var { accountType, accountTypes } = this.data;
    var obj = accountTypes.find(({ code }) => {
      return code == accountType;
    });
    if (obj) {
      var { name } = obj;
      this.setData({
        accountTypeName: name
      });
    }
  },

  onLoad(options) {
    this.getAccoutTypes(() => {
      var { type } = options;
      this.setData({
        seleted: type
      });
      var { items } = this.data;
      items.forEach(item => {
        item.checked = false;
        if (item.name == this.data.seleted) {
          console.log(item);
          item.checked = true;
        }
      });
      this.setData({
        items
      });
    });

  },

  onShow() {

  },

  getAccoutTypes(fn) {
    API.getAccoutTypes()
      .then(data => {
        this.setData({
          accountTypes: data
        });
        fn && fn()
      });
  },

  bindPickerChange(event) {
    var val = event.detail.value;
    var { code, name } = this.data.accountTypes[val];
    this.setData({
      accountType: code,
      accountTypeName: name
    })
  },

  bindDateChange(event) {
    var val = event.detail.value;
    this.setData({
      time: val
    })
  },

  addInput(event) {
    const accountVal = event.detail.value;
    this.setData({
      accountVal
    });
  },

  nameInput(event) {
    const accountName = event.detail.value;
    this.setData({
      accountName
    });
  },

  markInput(event) {
    const accountMark = event.detail.value;
    this.setData({
      accountMark
    });
  },


  timeInput(event) {
    const time = event.detail.value;
    this.setData({
      time
    });
  },

  checkVail() {
    var reg = /^[0-9]+.?[0-9]*$/;
    const { accountVal } = this.data;
    var bflag = true;
    var msg = '';
    do {
      if (!accountVal) {
        bflag = false;
        msg = '请填写消费金额';
        break;
      }
      if (!reg.test(accountVal)) {
        bflag = false;
        msg = '消费金额请填写数字';
        break;
      }
    } while (false);
    if (msg) {
      wx.showToast({
        icon: 'none',
        title: msg
      });
    }
    return bflag;
  },

  imageLoad(e) {
    var { height, width } = e.detail;
    var imgHeight = height * this.data.imgWidth / width;
    this.setData({
      imgHeight
    });
  },

  selectImg() {
    var openid = app.globalData.openid;
    var time = this.data.time;
    API.chooseImage({
      data: {
        sizeType: ['compressed']
      }
    }).then(chooseResult => {
      API.uploadFile({
        data: {
          cloudPath: `${openid}/${time}/${Date.now()}.png`,
          filePath: chooseResult.tempFilePaths[0]
        }
      }).then(res => {
        this.setData({
          fileid: res.fileID
        });
      });
    });
  },

  async sure() {
    var ischeck = this.checkVail();
    if (!ischeck) return;
    var { accountName, accountVal, accountMark, accountType, time, accountTypeName, fileid } = this.data;
    if (!accountName) {
      accountName = accountTypeName;
    }

    await http({
      action: APICONSTE.ADD_ACCOUNT,
      data: {
        accountMark,
        accountType,
        accountName,
        accountVal: accountVal / 1,
        time,
        fileid
      }
    });
    wx.navigateBack();
    this.setData({
      accountName: '',
      accountVal: 0
    });
  }
})